package com.imooc.rabbitmq.confirm;

import com.imooc.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;

import javax.validation.groups.Default;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consummer {
    private static final String exchangeName = "test_confirm_exchange";
    private static final String exchangeType = "topic";
    private static final String routingKey = "confirm.#";
    private static final String queueName = "test_confirm_queue";
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(exchangeName,exchangeType,true);
        channel.queueDeclare(queueName,true,false,false,null);
        channel.queueBind(queueName,exchangeName,routingKey);

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body);
                System.out.println("recv msg:"+msg);
            }
        };
        //监听队列
        channel.basicConsume(queueName,true, consumer);
    }
}
